package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.ProductMapper;
import com.kgc.service.IProductProviderService;
import entity.Machining;
import entity.Product;
import entity.ResponseMessage;
import entity.StatusCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;


/**
 * @program: product-provider
 * @description: 实现商品操作层
 * @author: Cheng
 * @create: 2022-04-16 14:32
 **/

@Service
@SuppressWarnings("all")
public class IProductProviderServiceImpl implements IProductProviderService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private ProductMapper productMapper;


    /**
     * 添加一条商品信息
     *
     * @param product
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage insertProduct(Product product) {
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            logger.info("IProductProviderServiceImpl insertProduct is start .... param:product-->" + product);
            int row = productMapper.insert(product);
            logger.debug("IProductProviderServiceImpl insertProduct result:row->"+row);
            //判断下结果,返回不同的数据
            if (row > 0) {
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setMessage("商品添加成功");
                responseMessage.setFlag(true);
                responseMessage.setData(row);
            } else {
                logger.error("IProductProviderServiceImpl insertProduct is error ,The row less than equals to zero");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("商品添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl insertProduct is error",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("添加商品运行时出现了一个错误");
        }
        return responseMessage;
    }


    /**
     * 异步检查名字是否存在
     * @param pname
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage insertPnameCheck(String pname) {
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            logger.info("IProductProviderServiceImpl insertPnameCheck is start .... param:pname-->" + pname);
            //查询名字存在数量>1则表示存在了
            int row = productMapper.insertPnameCheck(pname);
            logger.debug("IProductProviderServiceImpl insertPnameCheck result:row->"+row);
            //判断下结果,返回不同的数据
            if (row > 0) {
                logger.error("IProductProviderServiceImpl insertProduct is error ,The row less than equals to zero");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("该商品名已存在");
            } else {
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setFlag(true);
                responseMessage.setMessage("ok");
                responseMessage.setData(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl insertProduct is error",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("查询商品同名时出现了一个错误");
        }
        return responseMessage;
    }



    /**
     * 查询所有加工工艺信息
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectMachining() {
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            logger.info("IProductProviderServiceImpl selectMachining is start ....");
            //查询加工工艺信息
            List<Machining> productList = productMapper.selectMachining();
            logger.debug("IProductProviderServiceImpl selectMachining result:productList->"+productList);
            if (productList==null){
                logger.error("IProductProviderServiceImpl selectMachining is error , The param is nul");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("暂时没有加工工艺信息");
            }else {
                responseMessage.setMessage("查询全部工艺信息成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(productList);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl selectMachining is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IProductProviderServiceImpl selectMachining is error");
        }
        return responseMessage;
    }


    /**
     * 查询所有供应商的 id 和 公司名称 信息
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectProviderInfo() {
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            logger.info("IProductProviderServiceImpl selectProviderInfo is start ....");
            //查询供应商信息
            List<Machining> productList = productMapper.selectProviderInfo();
            logger.debug("IProductProviderServiceImpl selectProviderInfo result:productList->"+productList);
            if (productList==null){
                logger.error("IProductProviderServiceImpl selectProviderInfo is error , The result is nul");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("暂时没有供应商信息");
            }else {
                responseMessage.setMessage("查询全部供应商成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(productList);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl selectProviderInfo is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IProductProviderServiceImpl selectProviderInfo is error");
        }
        return responseMessage;
    }


    /**
     * 分页查询商品信息,联表分类表和供应商表
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage selectInfoByProductCategoryProvider(String providerId, String pname, String type, String status, String sort, int pageNum) {
        logger.info("IProductProviderServiceImpl selectInfoByProductCategoryProvider is start ......params:providerId-->"+
                providerId+",pname:-->"+pname+",type:->"+type+",status:->"+status+",sort:->"+sort+",pageNum:->"+pageNum);
        ResponseMessage responseMessage = new ResponseMessage();
        //添加查询的条件
        HashMap<String, Object> map = new HashMap<>();
        map.put("providerId",providerId);
        map.put("pname",pname);
        map.put("type",type);
        map.put("status",status);
        map.put("sort",sort);
        try {
            PageHelper pageHelper = new PageHelper();
            pageHelper.startPage(pageNum, 5);
            List<Product> productList = productMapper.selectInfoByProductCategoryProvider(map);
            PageInfo pageInfo = new PageInfo(productList);
            logger.debug("IProductProviderServiceImpl selectInfoByProductCategoryProvider result:"+productList);
            if (productList == null) {
                logger.error("The  IProductProviderServiceImpl selectInfoByProductCategoryProvider result: productList is null");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("The result: productList is null ");
            } else {
                responseMessage.setMessage("商品信息查询成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setFlag(true);
                responseMessage.setData(pageInfo);
            }
        } catch (Exception e) {
            logger.error("IProductProviderServiceImpl selectInfoByProductCategoryProvider is error ",e);
            e.printStackTrace();
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IProductProviderServiceImpl selectInfoByProductCategoryProvider is error");
        }
        return responseMessage;
    }


    /**
     * 通过id查询一条商品信息
     * @param id
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage selectProductById(String id) {
        logger.info("IProductProviderServiceImpl selectProductById is start .... param:id"+id);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //查询商品信息
            Product product = productMapper.selectProductById(id);
            logger.debug("IProductProviderServiceImpl selectProductById result:product->"+product);
            if (product==null){
                logger.error("IProductProviderServiceImpl selectProductById is error , The result is nul");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("暂时没有商品信息");
            }else {
                responseMessage.setMessage("查询商品信息成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(product);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl selectProductById is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IProductProviderServiceImpl selectProductById is error");
        }
        return responseMessage;
    }


    /**
     * 更新一条商品信息
     * @param product
     * @return  ResponseMessage
     */
    @Override
    public ResponseMessage updateProduct(Product product) {
        logger.info("IProductProviderServiceImpl selectProductById is start .... param:product"+product);
        ResponseMessage responseMessage = new ResponseMessage();
        try {
            //更新商品信息
            int updateCount = productMapper.updateProduct(product);
            logger.debug("IProductProviderServiceImpl selectProductById result:updateCount->"+updateCount);
            if (updateCount==0){
                logger.error("IProductProviderServiceImpl updateProduct is error , The result is equlas zero");
                responseMessage.setCode(StatusCode.ERROR);
                responseMessage.setMessage("商品信息更新失败");
            }else {
                responseMessage.setMessage("商品信息更新成功");
                responseMessage.setCode(StatusCode.OK);
                responseMessage.setData(updateCount);
                responseMessage.setFlag(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("IProductProviderServiceImpl updateProduct is error ",e);
            responseMessage.setCode(StatusCode.ERROR);
            responseMessage.setMessage("IProductProviderServiceImpl updateProduct is error");
        }
        return responseMessage;
    }


}
